﻿using JoeyLibs.IdleStartMenu.Model;
using JoeyLibs.Utility.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is IdleException)
            {
                IdleException exception = e.ExceptionObject as IdleException;
                new ExceptionLogModel(exception.Message, exception.Reason, exception.Solution, exception).Output();
            }
            else
            {
                new ExceptionLogModel(exception: e.ExceptionObject as Exception).Output();
            }            
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            if (e.Exception is IdleException)
            {
                IdleException exception = e.Exception as IdleException;
                new ExceptionLogModel(exception.Message, exception.Reason, exception.Solution, exception).Output();
            }
            else
            {
                new ExceptionLogModel(exception: e.Exception).Output();
            }
        }
    }
}
